﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using CooStationsToolsNew.Model;
using System.Data;

namespace CooStationsToolsNew
{
    /// <summary>
    /// WorkMenuAreaUC.xaml 的交互逻辑
    /// </summary>
    public partial class WorkMenuAreaUC : UserControl
    {
        Window win;
        ProjectTools pt = new ProjectTools();
        ModelTools model = new ModelTools();
        public WorkMenuAreaUC(Window _win)
        {
            InitializeComponent();
            win = _win;
        }
        private void lbtn_hidden_Click(object sender, RoutedEventArgs e)
        {

        }

        private void tabItem1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void btn_asave_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btn_open_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btn_close_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btn_Debug_Click(object sender, RoutedEventArgs e)
        {

        }

        private void item2_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void btn_copy_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btn_paste_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btn_all_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btn_del_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btn_ndiv_Click(object sender, RoutedEventArgs e)
        {
            Canvas canvasadd = null;
            string parentcan = null;
            if (App.proPath == null || App.proPath == "")
            {
                MessageForm1 mf = new MessageForm1("没有加载项目");
                mf.Owner = win;
                mf.ShowDialog();
                //System.Windows.MessageBox.Show("没有加载项目");
                return;
            }
            NewDivWin divwin = new NewDivWin();
            divwin.Owner = win;
            if (divwin.ShowDialog() == true)
            {
                Div div = new Div();
                div.Name = "d" + model.GetRandom();
                div.Id = divwin.divname_txt.Text;// "div" + model.SetDivID();
                div.Width = 500;
                div.Height = 500;
                div.X = divwin.thex;
                div.Y = divwin.they;
                div.Alpha = divwin.thealpha;
                if (App.uiobj==null )
                {
                    DataRow dr = App.dt.NewRow();
                    dr[0] = "div";
                    dr[1] = div;
                    App.dt.Rows.Add(dr);
                    canvasadd = model.CreatDiv(div, WorkMainAreaUC.workcanvas);
                    model.addDivtoxml(div, WorkMainAreaUC.workcanvas.Name);
                    parentcan = WorkMainAreaUC.workcanvas.Uid;
                }
                else
                {
                    if (App.uiobj.GetType() == typeof(Image))
                    {
                        canvasadd = model.CreatDiv(div, (Canvas)(App.uiobj as Image).Parent);
                        if ((Canvas)(App.uiobj as Image).Parent == WorkMainAreaUC.workcanvas)
                        {
                            DataRow dr = App.dt.NewRow();
                            dr[0] = "div";
                            dr[1] = div;
                            App.dt.Rows.Add(dr);
                            model.addDivtoxml(div, WorkMainAreaUC.workcanvas.Name);
                            parentcan = WorkMainAreaUC.workcanvas.Uid;
                        }
                        else
                        {
                            Div d = model.GetDiv((Canvas)(App.uiobj as Image).Parent, App.dt);
                            DataRow dr = d.Dt.NewRow();
                            dr[0] = "div";
                            dr[1] = div;
                            d.Dt.Rows.Add(dr);
                            model.addDivtoxml(div, ((Canvas)(App.uiobj as Image).Parent).Name);
                            parentcan = ((Canvas)(App.uiobj as Image).Parent).Uid;
                        }
                    }
                    else if (App.uiobj.GetType() == typeof(Rectangle))
                    {
                        canvasadd = model.CreatDiv(div, (Canvas)(App.uiobj as Rectangle).Parent);
                        if ((Canvas)(App.uiobj as Rectangle).Parent == WorkMainAreaUC.workcanvas)
                        {
                            DataRow dr = App.dt.NewRow();
                            dr[0] = "div";
                            dr[1] = div;
                            App.dt.Rows.Add(dr);
                            model.addDivtoxml(div, WorkMainAreaUC.workcanvas.Name);
                            parentcan = WorkMainAreaUC.workcanvas.Uid;
                        }
                        else
                        {
                            Div d = model.GetDiv((Canvas)(App.uiobj as Rectangle).Parent, App.dt);
                            DataRow dr = d.Dt.NewRow();
                            dr[0] = "div";
                            dr[1] = div;
                            d.Dt.Rows.Add(dr);
                            model.addDivtoxml(div, ((Canvas)(App.uiobj as Rectangle).Parent).Name);
                            parentcan = ((Canvas)(App.uiobj as Rectangle).Parent).Uid;
                        }
                    }
                    else if (App.uiobj.GetType() == typeof(TextBlock))
                    {
                        canvasadd = model.CreatDiv(div, (Canvas)(App.uiobj as TextBlock).Parent);
                        if ((Canvas)(App.uiobj as TextBlock).Parent == WorkMainAreaUC.workcanvas)
                        {
                            DataRow dr = App.dt.NewRow();
                            dr[0] = "div";
                            dr[1] = div;
                            App.dt.Rows.Add(dr);
                            model.addDivtoxml(div, WorkMainAreaUC.workcanvas.Name);
                            parentcan = WorkMainAreaUC.workcanvas.Uid;
                        }
                        else
                        {
                            Div d = model.GetDiv((Canvas)(App.uiobj as TextBlock).Parent, App.dt);
                            DataRow dr = d.Dt.NewRow();
                            dr[0] = "div";
                            dr[1] = div;
                            d.Dt.Rows.Add(dr);
                            model.addDivtoxml(div, ((Canvas)(App.uiobj as TextBlock).Parent).Name);
                            parentcan = ((Canvas)(App.uiobj as TextBlock).Parent).Uid;
                        }
                    }
                    else if (App.uiobj.GetType() == typeof(Canvas))
                    {
                        canvasadd = model.CreatDiv(div, App.uiobj as Canvas);
                        Div d = model.GetDiv(App.uiobj as Canvas, App.dt);
                        DataRow dr = d.Dt.NewRow();
                        dr[0] = "div";
                        dr[1] = div;
                        d.Dt.Rows.Add(dr);
                        model.addDivtoxml(div, (App.uiobj as Canvas).Name);
                        parentcan = (App.uiobj as Canvas).Uid;
                    }
                }
                AddPageObjTreeView(canvasadd, parentcan);
            }
           // MainWindow.workleftuc.BindingTreeView(WorkMainAreaUC.workcanvas);
        }

        private void btn_new_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btn_nimg_Click(object sender, RoutedEventArgs e)
        {
            if (App.proPath == null || App.proPath == "")
            {
                MessageForm1 mf = new MessageForm1("没有加载项目");
                mf.Owner = win;
                mf.ShowDialog();
                return;
            }
            ResourceManagerWin rmWin = new ResourceManagerWin("image");
            rmWin.Owner = win;
            rmWin.ShowDialog();
            if (App.NewImage != "" && App.NewImage != null)
            {
                AddImage();
            }
            if (App.NewRect == "rectangle")
            {
                AddRect();
            }
           //MainWindow.workleftuc.BindingTreeView(WorkMainAreaUC.workcanvas);
        }

        public void AddPageObjTreeView(UIElement objadd, string parentcan)
        {
            if (objadd != null)
            {
                if (parentcan == null || parentcan == "")
                {
                    MainWindow.workleftuc.AddTreeView(MainWindow.workleftuc.treeView1.Items[0] as TreeViewItem, objadd);
                }else 
                {
                    TreeViewItem tvi = CheckOTV(MainWindow.workleftuc.treeView1.Items[0] as TreeViewItem, parentcan);
                    if (tvi != null)
                    {
                        MainWindow.workleftuc.AddTreeView(tvi, objadd);
                    }
                    
                }
            }
        }

       
        /// <summary>
        /// 获得TreeViewItem
        /// </summary>
        /// <param name="tvi"></param>
        /// <param name="oldName"></param>
        /// <returns></returns>
        public TreeViewItem CheckOTV(TreeViewItem tvi, string oldName)
        {
            TreeViewItem treeviewitem = null;
            if (tvi == null)
            {

                tvi = (TreeViewItem)MainWindow.workleftuc.treeView1.Items[0];
                for (int i = 0; i < tvi.Items.Count; i++)
                {
                    TreeViewItem theTree = (TreeViewItem)tvi.Items[i];
                    ObjectTreeViewUC theObjTree = (ObjectTreeViewUC)theTree.Header;
                    if (theObjTree.txtb.Text == oldName)
                    {
                        treeviewitem = theTree;
                        // theObjTree.txtb.Text = newName;
                        //theObjTree.UpdateLayout();
                        break;
                    }
                    else
                    {
                        if (theTree.Items.Count != 0)
                        {
                            treeviewitem = CheckOTV(theTree, oldName);
                        }
                    }
                }
            }
            else
            {

                for (int i = 0; i < tvi.Items.Count; i++)
                {
                    TreeViewItem theTree = (TreeViewItem)tvi.Items[i];
                    ObjectTreeViewUC theObjTree = (ObjectTreeViewUC)theTree.Header;
                    if (theObjTree.txtb.Text == oldName)
                    {
                        treeviewitem = theTree;
                        //theObjTree.upDataName(newName);
                        break;
                    }
                    else
                    {
                        if (theTree.Items.Count != 0)
                        {
                            treeviewitem = CheckOTV(theTree, oldName);
                        }
                    }
                }
            }
            return treeviewitem;
        }
        /// <summary>
        /// 插入图片方法
        /// </summary>
        private void AddImage()
        {
            Image imgadd = null;
            string newImage = App.NewImage;
            System.IO.FileInfo f = new System.IO.FileInfo(newImage);
            string parentcan = null;
            if (f.Exists)
            {
                System.Drawing.Bitmap b = new System.Drawing.Bitmap(newImage);
                if (f.Directory.ToString() != App.proPath + "\\pic\\")
                {
                    string newPath = "";
                    try
                    {
                        if (File.Exists(App.appPath + "//temp//pic//" + f.Name))
                        {
                            newPath = App.appPath + "//temp//pic//" + f.Name;
                            //File.Copy(newImage, newPath);
                        }
                        else
                        {
                            newPath = App.appPath + "//temp//pic//" + f.Name;
                            File.Copy(newImage, App.appPath + "//temp//pic//" + f.Name, true);
                        }
                    }
                    catch (Exception e)
                    { }
                    Pic pic = new Pic();
                    pic.Name = "p" + pt.GetRandom();
                    pic.Id = "image" + model.SetImageID();
                    pic.X = 0;
                    pic.Y = 0;
                    pic.Height = b.Height;
                    pic.Width = b.Width;
                    pic.Alpha = 100;
                    pic.Url = newPath;
                    if (App.uiobj == null) {
                        
                            imgadd = model.CreateImage(pic, WorkMainAreaUC.workcanvas);
                            DataRow dr = App.dt.NewRow();
                            dr[0] = "image";
                            dr[1] = pic;
                            App.dt.Rows.Add(dr);
                            model.addImgtoxml(pic, WorkMainAreaUC.workcanvas.Name);
                            parentcan = WorkMainAreaUC.workcanvas.Uid;
                            AddPageObjTreeView(imgadd, parentcan);
                        return;
                    }
                    else if (App.uiobj.GetType() == typeof(Canvas))
                    {
                        Canvas divpcan = (Canvas)(App.uiobj as Canvas).Parent;
                        if (divpcan.Parent.GetType() == typeof(Border))
                        {
                            imgadd = model.CreateImage(pic, WorkMainAreaUC.workcanvas);
                            DataRow dr = App.dt.NewRow();
                            dr[0] = "image";
                            dr[1] = pic;
                            App.dt.Rows.Add(dr);
                            model.addImgtoxml(pic, WorkMainAreaUC.workcanvas.Name);
                            parentcan = WorkMainAreaUC.workcanvas.Uid;
                            AddPageObjTreeView(imgadd, parentcan);
                        } return;
                    }
                    else
                    {
                        if (App.uiobj.GetType()==typeof(Image))
                        {
                            Canvas imgpcan = (Canvas)(App.uiobj as Image).Parent;
                            if (imgpcan.Name == WorkMainAreaUC.workcanvas.Name)
                            {
                                imgadd = model.CreateImage(pic, WorkMainAreaUC.workcanvas);
                                DataRow dr = App.dt.NewRow();
                                dr[0] = "image";
                                dr[1] = pic;
                                App.dt.Rows.Add(dr);
                                model.addImgtoxml(pic, WorkMainAreaUC.workcanvas.Name);
                                parentcan = WorkMainAreaUC.workcanvas.Uid;
                            }
                            else
                            {
                                imgadd = model.CreateImage(pic, imgpcan);
                                Div d = model.GetDiv(imgpcan, App.dt);
                                DataRow dr = d.Dt.NewRow();
                                dr[0] = "image";
                                dr[1] = pic;
                                d.Dt.Rows.Add(dr);
                                model.addImgtoxml(pic, imgpcan.Name);
                                parentcan = imgpcan.Uid;
                            }
                        }
                        else if (App.uiobj.GetType() == typeof(Rectangle))
                        {
                            Canvas rectpcan = (Canvas)(App.uiobj as Rectangle).Parent;
                            if (rectpcan.Name == WorkMainAreaUC.workcanvas.Name)
                            {
                                imgadd = model.CreateImage(pic, WorkMainAreaUC.workcanvas);
                                DataRow dr = App.dt.NewRow();
                                dr[0] = "image";
                                dr[1] = pic;
                                App.dt.Rows.Add(dr);
                                model.addImgtoxml(pic, WorkMainAreaUC.workcanvas.Name);
                                parentcan = WorkMainAreaUC.workcanvas.Uid;
                            }
                            else
                            {
                                imgadd = model.CreateImage(pic, rectpcan);
                                Div d = model.GetDiv(rectpcan, App.dt);
                                DataRow dr = d.Dt.NewRow();
                                dr[0] = "image";
                                dr[1] = pic;
                                d.Dt.Rows.Add(dr);
                                model.addImgtoxml(pic, rectpcan.Name);
                                parentcan = rectpcan.Uid;
                            }
                        }
                        else if (App.uiobj.GetType() == typeof(TextBlock))
                        {
                            Canvas txtpcan = (Canvas)(App.uiobj as TextBlock).Parent;
                            if (txtpcan.Name == WorkMainAreaUC.workcanvas.Name)
                            {
                                imgadd = model.CreateImage(pic, WorkMainAreaUC.workcanvas);
                                DataRow dr = App.dt.NewRow();
                                dr[0] = "image";
                                dr[1] = pic;
                                App.dt.Rows.Add(dr);
                                model.addImgtoxml(pic, WorkMainAreaUC.workcanvas.Name);
                                parentcan = WorkMainAreaUC.workcanvas.Uid;
                            }
                            else
                            {
                                imgadd = model.CreateImage(pic, txtpcan);
                                Div d = model.GetDiv(txtpcan, App.dt);
                                DataRow dr = d.Dt.NewRow();
                                dr[0] = "image";
                                dr[1] = pic;
                                d.Dt.Rows.Add(dr);
                                model.addImgtoxml(pic, txtpcan.Name);
                                parentcan = txtpcan.Uid;
                            }
                        }
                        else if (App.uiobj.GetType() == typeof(Canvas))
                        {
                            imgadd = model.CreateImage(pic, App.uiobj as Canvas);
                            Div d = model.GetDiv(App.uiobj as Canvas, App.dt);
                            DataRow dr = d.Dt.NewRow();
                            dr[0] = "image";
                            dr[1] = pic;
                            d.Dt.Rows.Add(dr);
                            model.addImgtoxml(pic, (App.uiobj as Canvas).Name);
                            parentcan = (App.uiobj as Canvas).Uid;
                        }
                        else
                        {
                            if (App.NewImage != "" & App.NewImage != null)
                            {
                                imgadd = model.CreateImage(pic, WorkMainAreaUC.workcanvas);
                                DataRow dr = App.dt.NewRow();
                                dr[0] = "image";
                                dr[1] = pic;
                                App.dt.Rows.Add(dr);
                                model.addImgtoxml(pic, WorkMainAreaUC.workcanvas.Name);
                                parentcan = WorkMainAreaUC.workcanvas.Uid;
                            }
                        }AddPageObjTreeView(imgadd, parentcan);
                    }
                  
                } 
                
            }

        
            App.NewImage = "";
        }

        /// <summary>
        /// 插入矩形方法
        /// </summary>
        private void AddRect()
        {
            Rectangle rectadd = null;
            string parentcan = null;
            if (App.NewRect == "rectangle")
            {
                if (App.proPath == null || App.proPath == "")
                {
                    MessageForm1 mf = new MessageForm1("没有加载项目");
                    mf.Owner = win;
                    mf.ShowDialog();
                    return;
                }
                CooStationsToolsNew.Model.Rect re = new CooStationsToolsNew.Model.Rect();
                re.Name = "r" + model.GetRandom();
                re.Id = "rect" + model.SetRectID();
                re.Width = 150;
                re.Height = 150;
                re.X = 0;
                re.Y = 0;
                re.Border = 1;
                re.RectColor = "0xFFFFFF";
                re.RectAlpha = 100;
                re.LineAlpha = 100;
                re.LineColor = "0x000000";
                if (App.uiobj == null) {

                    rectadd = model.CreatRect(re, WorkMainAreaUC.workcanvas);
                    DataRow dr = App.dt.NewRow();
                    dr[0] = "rect";
                    dr[1] = re;
                    App.dt.Rows.Add(dr);
                    model.addRecttoxml(re, WorkMainAreaUC.workcanvas.Name);
                    parentcan = WorkMainAreaUC.workcanvas.Uid;
                    AddPageObjTreeView(rectadd, parentcan);
                    return;
                }
                else if (App.uiobj.GetType() == typeof(Canvas))
                {
                    Canvas divpcan = (Canvas)(App.uiobj as Canvas).Parent;
                    if (divpcan.Parent.GetType() == typeof(Border))
                    {
                        rectadd = model.CreatRect(re, WorkMainAreaUC.workcanvas);
                        DataRow dr = App.dt.NewRow();
                        dr[0] = "rect";
                        dr[1] = re;
                        App.dt.Rows.Add(dr);
                        model.addRecttoxml(re, WorkMainAreaUC.workcanvas.Name);
                        parentcan = WorkMainAreaUC.workcanvas.Uid;
                        AddPageObjTreeView(rectadd, parentcan);
                    } return;
                }
                else
                {
                    if (App.uiobj.GetType() == typeof(Image))
                    {
                        Canvas imgpcan = (Canvas)(App.uiobj as Image).Parent;
                        if (imgpcan == WorkMainAreaUC.workcanvas)
                        {
                            rectadd = model.CreatRect(re, WorkMainAreaUC.workcanvas);
                            DataRow dr = App.dt.NewRow();
                            dr[0] = "rect";
                            dr[1] = re;
                            App.dt.Rows.Add(dr);
                            model.addRecttoxml(re, WorkMainAreaUC.workcanvas.Name);
                            parentcan = WorkMainAreaUC.workcanvas.Uid;
                        }
                        else
                        {
                            rectadd = model.CreatRect(re, imgpcan);
                            Div d = model.GetDiv(imgpcan, App.dt);
                            DataRow dr = d.Dt.NewRow();
                            dr[0] = "rect";
                            dr[1] = re;
                            d.Dt.Rows.Add(dr);
                            model.addRecttoxml(re, imgpcan.Name);
                            parentcan = imgpcan.Uid;
                        }
                    }
                    else if (App.uiobj.GetType() == typeof(Rectangle))
                    {
                        Canvas rectpcan = (Canvas)(App.uiobj as Rectangle).Parent;
                        if (rectpcan == WorkMainAreaUC.workcanvas)
                        {
                            rectadd = model.CreatRect(re, WorkMainAreaUC.workcanvas);
                            DataRow dr = App.dt.NewRow();
                            dr[0] = "rect";
                            dr[1] = re;
                            App.dt.Rows.Add(dr);
                            model.addRecttoxml(re, WorkMainAreaUC.workcanvas.Name);
                            parentcan = WorkMainAreaUC.workcanvas.Uid;
                        }
                        else
                        {
                            rectadd = model.CreatRect(re, rectpcan);
                            Div d = model.GetDiv(rectpcan, App.dt);
                            DataRow dr = d.Dt.NewRow();
                            dr[0] = "rect";
                            dr[1] = re;
                            d.Dt.Rows.Add(dr);
                            model.addRecttoxml(re, rectpcan.Name);
                            parentcan = rectpcan.Uid;
                        }
                    }
                    else if (App.uiobj.GetType() == typeof(TextBlock))
                    {
                        Canvas txtpcan = (Canvas)(App.uiobj as TextBlock).Parent;
                        if (txtpcan == WorkMainAreaUC.workcanvas)
                        {
                            rectadd = model.CreatRect(re, WorkMainAreaUC.workcanvas);
                            DataRow dr = App.dt.NewRow();
                            dr[0] = "rect";
                            dr[1] = re;
                            App.dt.Rows.Add(dr);
                            model.addRecttoxml(re, WorkMainAreaUC.workcanvas.Name);
                            parentcan = WorkMainAreaUC.workcanvas.Uid;
                        }
                        else
                        {
                            rectadd = model.CreatRect(re, txtpcan);
                            Div d = model.GetDiv(txtpcan, App.dt);
                            DataRow dr = d.Dt.NewRow();
                            dr[0] = "rect";
                            dr[1] = re;
                            d.Dt.Rows.Add(dr);
                            model.addRecttoxml(re, txtpcan.Name);
                            parentcan = txtpcan.Uid;
                        }
                    }
                    else if (App.uiobj.GetType() == typeof(Canvas))
                    {
                        rectadd = model.CreatRect(re, App.uiobj as Canvas);
                        Div d = model.GetDiv(App.uiobj as Canvas, App.dt);
                        DataRow dr = d.Dt.NewRow();
                        dr[0] = "rect";
                        dr[1] = re;
                        d.Dt.Rows.Add(dr);
                        model.addRecttoxml(re, (App.uiobj as Canvas).Name);
                        parentcan = (App.uiobj as Canvas).Uid;
                    }
                    else
                    {
                        if (App.NewRect == "rectangle")
                        {
                            rectadd = model.CreatRect(re, WorkMainAreaUC.workcanvas);
                            DataRow dr = App.dt.NewRow();
                            dr[0] = "rect";
                            dr[1] = re;
                            App.dt.Rows.Add(dr);
                            model.addRecttoxml(re, WorkMainAreaUC.workcanvas.Name);
                            parentcan = WorkMainAreaUC.workcanvas.Uid;
                        }
                    }
                    AddPageObjTreeView(rectadd, parentcan);
                }
            }
        }

        private void btn_cut_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btn_nshape_Click(object sender, RoutedEventArgs e)
        {
            if (App.proPath == null || App.proPath == "")
            {
                MessageForm1 mf = new MessageForm1("没有加载项目");
                mf.Owner = win;
                mf.ShowDialog();
                return;
            }
            ResourceManagerWin rmWin = new ResourceManagerWin("rectangle");
            rmWin.Owner = win;
            rmWin.ShowDialog();
            if (App.NewRect == "rectangle")
            {
                AddRect();
            }
            if (App.NewImage != "" && App.NewImage != null)
            {
                AddImage();
            }

            //MainWindow.workleftuc.BindingTreeView(WorkMainAreaUC.workcanvas);
        }

        //插入文字
        private void btn_ntxt_Click(object sender, RoutedEventArgs e)
        {
            //if (App.proPath == null || App.proPath == "")
            //{
            //    MessageForm1 mf = new MessageForm1("没有加载项目");
            //    mf.Owner = win;
            //    mf.ShowDialog();
            //    //System.Windows.MessageBox.Show("没有加载项目");
            //    return;
            //}
            
            //if (textAC != null)
            //{
            //    if (ModelTools.autoRTB != null)
            //    {
            //        TextBlock tb = ModelTools.autoRTB.Tag as TextBlock;
            //        Canvas can = tb.Parent as Canvas;
            //        can.Children.Remove(ModelTools.autoRTB);
            //        ModelTools.autoRTB = null;
            //    }
            //    textAC.Visibility = Visibility.Visible;
            //    textAC.TextAlignment = TextAlignment.Left;
            //    textAC.Text = textAC.Text;
            //    model.SaveToTextData(ModelTools.pessText, App.dt);
            //    DelBindingText(textAC);
            //    BindingText(textAC);
            //}

            TextBlock txtadd = null;
            string parentcan = null;
            if (App.proPath == null || App.proPath == "")
            {
                MessageForm1 mf = new MessageForm1("没有加载项目");
                mf.Owner = win;
                mf.ShowDialog();
                //System.Windows.MessageBox.Show("没有加载项目");
                return;
            }
            Model.Text text = new Model.Text();
            text.Name = "text" + model.GetRandom();
            text.Id = "text" + model.SetTextID();
            text.Width = 100;
            text.Height = 25;
            text.X = 0;
            text.Y = 0;
            text.Content = text.Id;
            text.Multiline = "false";
            text.Align = "left";
            text.Size = "25";
            text.Bold = "false";
            text.Color = "0x000000";
            if (App.uiobj==null)
            {
                txtadd = model.CreatText(text, WorkMainAreaUC.workcanvas);
                DataRow dr = App.dt.NewRow();
                dr[0] = "text";
                dr[1] = text;
                App.dt.Rows.Add(dr);
                model.addTexttoxml(text, WorkMainAreaUC.workcanvas.Name);
                parentcan = WorkMainAreaUC.workcanvas.Uid;
            }
            else
            {
                if (App.uiobj.GetType()==typeof(Image))
                {
                    if ((Canvas)(App.uiobj as Image).Parent == WorkMainAreaUC.workcanvas)
                    {
                        txtadd = model.CreatText(text, WorkMainAreaUC.workcanvas);
                        DataRow dr = App.dt.NewRow();
                        dr[0] = "text";
                        dr[1] = text;
                        App.dt.Rows.Add(dr);
                        model.addTexttoxml(text, WorkMainAreaUC.workcanvas.Name);
                        parentcan = WorkMainAreaUC.workcanvas.Uid;
                    }
                    else
                    {
                        txtadd = model.CreatText(text, (Canvas)(App.uiobj as Image).Parent);
                        Div d = model.GetDiv((Canvas)(App.uiobj as Image).Parent, App.dt);
                        DataRow dr = d.Dt.NewRow();
                        dr[0] = "text";
                        dr[1] = text;
                        d.Dt.Rows.Add(dr);
                        model.addTexttoxml(text, ((Canvas)(App.uiobj as Image).Parent).Name);
                        parentcan = ((Canvas)(App.uiobj as Image).Parent).Uid;
                    }
                }
                else if (App.uiobj.GetType() == typeof(Rectangle))
                {
                    if ((Canvas)(App.uiobj as Rectangle).Parent == WorkMainAreaUC.workcanvas)
                    {
                        txtadd = model.CreatText(text, WorkMainAreaUC.workcanvas);
                        DataRow dr = App.dt.NewRow();
                        dr[0] = "text";
                        dr[1] = text;
                        App.dt.Rows.Add(dr);
                        model.addTexttoxml(text, WorkMainAreaUC.workcanvas.Name);
                        parentcan = WorkMainAreaUC.workcanvas.Uid;
                    }
                    else
                    {
                        txtadd = model.CreatText(text, (Canvas)(App.uiobj as Rectangle).Parent);
                        Div d = model.GetDiv((Canvas)(App.uiobj as Rectangle).Parent, App.dt);
                        DataRow dr = d.Dt.NewRow();
                        dr[0] = "text";
                        dr[1] = text;
                        d.Dt.Rows.Add(dr);
                        model.addTexttoxml(text, ((Canvas)(App.uiobj as Rectangle).Parent).Name);
                        parentcan = ((Canvas)(App.uiobj as Rectangle).Parent).Uid;
                    }
                }
                else if (App.uiobj.GetType() == typeof(TextBlock))
                {
                    if ((Canvas)(App.uiobj as TextBlock).Parent == WorkMainAreaUC.workcanvas)
                    {
                        txtadd = model.CreatText(text, WorkMainAreaUC.workcanvas);
                        DataRow dr = App.dt.NewRow();
                        dr[0] = "text";
                        dr[1] = text;
                        App.dt.Rows.Add(dr);
                        model.addTexttoxml(text, WorkMainAreaUC.workcanvas.Name);
                        parentcan = WorkMainAreaUC.workcanvas.Uid;
                    }
                    else
                    {
                        txtadd = model.CreatText(text, (Canvas)(App.uiobj as TextBlock).Parent);
                        Div d = model.GetDiv((Canvas)(App.uiobj as TextBlock).Parent, App.dt);
                        DataRow dr = d.Dt.NewRow();
                        dr[0] = "text";
                        dr[1] = text;
                        d.Dt.Rows.Add(dr);
                        model.addTexttoxml(text, ((Canvas)(App.uiobj as TextBlock).Parent).Name);
                        parentcan = ((Canvas)(App.uiobj as TextBlock).Parent).Uid;
                    }
                }
                else if (App.uiobj.GetType() == typeof(Canvas))
                {
                    txtadd = model.CreatText(text, App.uiobj as Canvas);
                    Div d = model.GetDiv(App.uiobj as Canvas, App.dt);
                    DataRow dr = d.Dt.NewRow();
                    dr[0] = "text";
                    dr[1] = text;
                    d.Dt.Rows.Add(dr);
                    model.addTexttoxml(text, (App.uiobj as Canvas).Name);
                    parentcan = (App.uiobj as Canvas).Uid;
                }
            }
            AddPageObjTreeView(txtadd, parentcan);
        }

        private void btn_nvideo_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btn_save_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btn_action_Click(object sender, RoutedEventArgs e)
        {
            CheckEdit ce = new CheckEdit(); 
            ce.Owner = win;
            ce.ShowDialog();
        }

        private void btn_npage_Click(object sender, RoutedEventArgs e)
        {
            //打开插入页面的窗口
            AddNewPageWin newpage = new AddNewPageWin();
            newpage.Owner = win;
            if (newpage.ShowDialog() == true)
            {
                bool ishaved = false;
                for (int i = 0; i < App.Pagelist.Count; i++)
                {
                    Model.CfyPage thiscp = App.Pagelist[i] as Model.CfyPage;
                    if (App.cpage.PageName == thiscp.PageName)
                    {
                        ishaved = true;
                    }
                }
                if (!ishaved)
                {
                    MainWindow.workmainuc.ToNewPage();
                }
                else
                {
                    MessageForm1 mf = new MessageForm1(App.cpage.PageName + "已存在，请重新命名");
                    mf.Owner = win;
                    mf.ShowDialog();
                    //System.Windows.MessageBox.Show(App.cpage.PageName+"已存在，请重新命名");
                }
            }
        }

        private void btn_lastu_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btn_animation_Click(object sender, RoutedEventArgs e)
        {

        }

        private void item4_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void itemSet_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void item3_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {

        }
    }
}
